#!/bin/bash

USAGE="$0 <DATABASE> <USER> <PASSWORD>"
DATABASE=$1
USER=$2
PASS=$3
MYSQL=/usr/bin/mysql
SCRIPTS=./database

if [ -z $1 ] || [ -z $2 ]; then
	echo "Invalid option. Usage: $USAGE"
	exit 1
fi

if [ -z $3 ]; then
	SQLEX="$MYSQL -u$USER"
else
	SQLEX="$MYSQL -u$USER -p$PASS"
fi

echo -n "Deleting database '$DATABASE': "
echo "DROP DATABASE IF EXISTS $DATABASE" | $SQLEX

if [ $? != 0 ]; then
	echo "Error deleting database!"
	exit 1
else
	echo "OK"
fi

echo -n "Creating database '$DATABASE': "
echo "CREATE DATABASE $DATABASE CHARACTER SET utf8 COLLATE utf8_general_ci" | $SQLEX

if [ $? != 0 ]; then
	echo "Error creating database!"
	exit 1
else
	echo "OK"
fi

echo -n "Creating schema: "
$SQLEX $DATABASE < "database.mysql"

if [ $? != 0 ]; then
	echo "Error creating schema!"
	exit 1
else
	echo "OK"
fi

echo "All done!"